<?php
namespace bankworking\bank;

use bankworking\credit\Deposit;

class Bank {
	
	const HOME_CREDIT = "HOME_CREDIT";
	const CONSUMER_CREDIT = "CONSUMER_CREDIT";
	
	private $name;
	private $address;
	private $moneyInASave;
	private $listOfDeposit = array();
	private $listOfCredit = array();
	
	
	public function __construct($name, $address, $moneyInASave){
		$this->setName($name);
		$this->setAddress($address);
		$this->moneyInASave = $moneyInASave;
	}
	
	public function setName($newName){
		try {
			if ($newName == "" || $newName == null){
				throw new Exception("Please, enter Bank name!");
			} else if (is_numeric($newName)) {
				throw new Exception("Bank name not be only numbers!");
			} else {
				$this->name = $newName;
				return true;
			}
		} catch (Exception $e) {
			echo "<p>" . $e->getMessage() . "</p>";
		}
	
	}
	
	public function getName(){
		return $this->name;
	}
	
	public function setAddress($newAddress){
		try {
			if ($newAddress == "" || $newAddress == null){
				throw new Exception("Please, enter address!");
			} else if (is_numeric($newAddress)) {
				throw new Exception("Address not be only numbers!");
			} else {
				$this->address = $newAddress;
				return true;
			}
		} catch (Exception $e) {
			echo "<p>" . $e->getMessage() . "</p>";
		}
	}
	
	public function getAddress(){
		return $this->address;
	}
	
	public function setMoneyInASafe($newMoneyInASave){
		$this->moneyInASave = $newMoneyInASave;
	}
	
	public function getMoneyInASave(){
		return $this->moneyInASave;
	}
	
	
	public function setListOfDeposit(Deposit $deposit){
		$name = $deposit->getName();
		$this->listOfDeposit[$name] = $deposit;
	}
	
	public function giveCredit($amount, $period, Client $client, $creditType)
	{
		// Checks
		$totalCreditPayment = 0;
	
		foreach ($client->getCredits() as $credit)
		{
			$totalCreditPayment += $credit->getPayment();
		}
	
		$creditPayment = BankUtils::calculateCreditPayment($amount, "%", $period);
	
		if(0.5 * $client->getSalary() < $totalCreditPayment + $creditPayment)
		{
			throw new Exception("Problem here");
		}
	
		$bankReserve = 0;
	
		foreach ($this->getDeposits() as $deposit)
		{
			$bankReserve += $deposit->getAmount();
		}
	
		$bankReserve *= 0.10;
		if($this->bankMoney - $amount < $bankReserve)
		{
			throw new Exception("Problem here");
		}
	
		// Object creating
		if($creditType == self::HOME_CREDIT)
		{
			$clientCredit = new HomeCredit($amount, $period);
				
		}else if($creditType == self::CONSUMER_CREDIT)
		{
			$clientCredit = new ConsumerCredit($amount, $period);
		}
	
		$client->addCredit($clientCredit);
		$this->addCredit($clientCredit);
	
	}
	
	

	public function showBankInfo(){
		echo "--------------Bank Info----------------------<br />";
		echo "Name: ". $this->name ."<br />";
		echo "Address: ". $this->address ."<br />";
		echo "Money in a safe: ". $this->moneyInASafe ."<br />";
	
		echo "---------------End------------------------------<br />";
	}
	
	
	
}